// 05shoppingMall/01commodityDetail/commodityDetail.js
var WxParse = require('../../wxParse/wxParse.js')
var co = require('../../resource/js/common.js')
var app = getApp()
var _this
var Products_ID
var _data
Page({
   data: {
      detail: '', //商品详情
      comment: '',
      isDisplay: '',
      showModal: false, //显示模态框
      objs: '', //商品的规格
      numbers: 1, //购买数量
      id: '', //规格id
      user: '', //用户信息
      inventory: '', //库存
      unitPrice: '', //单价
      option: '', //判断点击的是加入购物车还是立即购买
      skuName: '', //选中的规格名称       
   },

   onLoad: function(options) {
      _this = this
      _data = this.data
      console.log('app.Data', app.Data)
      //列表页传过来的商品Id
      Products_ID = options.Products_ID
      var user = app.Data.userInfo
      this.setData({
         user: user
      })
      // 商品详情
      this.getProductList()
      //获取商家信息
      // 评论
      //this.getProductComment()
      wx.showShareMenu({
         withShareTicket: true
      })
      //将邀请人ID保存全局变量
      if (options.User_ID) {
         wx.setStorageSync('Owner_Id', options.User_ID)
      }
      console.log('【详情页参数options】==', options)
      if (app.Data.userInfo == '') {
         app.loginStatic()
      }
   },
   // 返回执行添加商品
   onShow: function() {
      console.log(app.Data.userInfo)
      this.setData({
         user: app.Data.userInfo
      })
   },
   getProductList() {
      var param = {
         Products_ID: Products_ID
      }
      co.getMealApi('jyhs/Prdmg/getList', param, function(res) {
         if (res.code == 0) {
            _this.setData({
               detail: res.data,
               inventory: res.data.Products_Count,
               unitPrice: res.data.Products_PriceX
            })
            console.log('res.data', res.data)
            //_this.dataToView(res.data.Products_Details)
            var obj = res.data.sku_list
            console.log("【========================】", obj)
            // _this.toDetailSpec(obj)
            _this.setData({
               objs: obj
            })
         }
      })

   },
   // 跳转到购物车页面
   ToshoppingCar: function() {
      wx.switchTab({
         url: '/06shoppingCart/shoppingCart',
      })
   },
   // toStore: function () {
   //    console.log("【Biz_ID】", this.data.detail)
   //    wx.navigateTo({
   //       url: '/05local/02shopHome/shopHome?Biz_ID=' + this.data.detail.Biz_ID,
   //    })
   // },
   //处理接收详情的span对象，key商品规格下标
   toDetailSpec: function(obj) {
      _this = this
      for (var key in obj) {
         _this.setData({
            objs: obj[key]
         })
         console.log("【规格规格===========】", obj[key])
      }
   },
   // 选择商品规格
   choose: function(e) {
      // var oIndex = e.currentTarget.dataset.oindex,
      //    idx = e.currentTarget.dataset.idx;

      // var id = this.data.id, list = this.data.detail.sku_list;
      // console.log('list',list)
      // id[oIndex] = idx
      // this.setData({
      //    id: id
      // })
      // console.log("【id===】", id)
      // var gindex = "";
      // var skuName = "";
      // console.log("【111111111】", list)
      // for (var i = 0; i < id.length; i++) {
      //    skuName += list[i].sku[id[i]] + "  "
      //    console.log("【循环中的值==】", skuName)
      //    if (i > 0) {
      //       gindex += ";" + id[i];
      //    } else {
      //       gindex += id[i];
      //    }
      // }
      // console.log("【skuName===】", skuName)
      // console.log("下标", gindex)
      // this.setData({
      //    skuName: skuName
      // })
      // var guige = this.data.detail.skuval
      // console.log("【规格1231232】", guige[gindex])
      // if (guige[gindex] != undefined) {
      //    console.log("123", guige[gindex].Txt_CountSon, guige[gindex].Txt_PriceSon)
      //    this.setData({
      //       inventory: guige[gindex].Txt_CountSon,
      //       unitPrice: guige[gindex].Txt_PriceSon
      //    })
      // }
      var idx = e.currentTarget.dataset.idx
      var obj = _this.data.detail.sku_list[idx]
      console.log('e', e)
      _this.setData({
         id: idx,
         skuName: obj.Sku_Name,
         inventory: obj.Products_Count,
         unitPrice: obj.Products_PriceX
      })
   },
   // 处理富文本
   dataToView(data) {
      console.log('into')
      var article = data
      var that = this;
      WxParse.wxParse('article', 'html', article, that, 5);
   },
   // 点击确定
   ToOrderOnshoppingCart: function() {
      console.log('oooooption', this.data.option)
      if (this.data.option == "purchase") {
         this.ToOrder()
      } else {
         this.addShoppingCart()
      }
   },
   // 跳转到订单页
   ToOrder: function() {
      var detail = this.data.detail
      //console.log('detail',detail)
      console.log('id', this.data)
      if (this.data.id === '' && !co.empty(this.data.detail.sku_list)) {
         console.log("【请选择规格】", this.data.id, detail.sku);
         co.toast("请选择规格")
      } else if (this.data.inventory < this.data.numbers) {
         co.toast("库存不足")
      } else {
         var select = _this.data.detail
         //console.log('app.Data', app.Data)
         console.log('select', select)
         var param = [{
            User_ID: app.Data.userInfo.User_ID,
            Order_CartList: [{
               Products_ID: select.Products_ID,
               Products_Photo: select.Products_JSON.ImgPath[0],
               Products_Name: select.Products_Name,
               Products_Price: this.data.unitPrice,
               num: this.data.numbers,
               sku_name: this.data.skuName,
               sku_id: this.data.id,
               UserName: app.Data.userInfo.User_NickName,
               unionid: app.Data.userInfo.unionid
            }]
         }]
         param = JSON.stringify(param)
         console.log('规格',param)
         // wx.navigateTo({
         //    url: '/06shoppingCart/01orderBefor/orderBefor?param=' + param,
         // })
      }
   },
   // 添加购物车
   addShoppingCart: function() {
      if (_this.data.id == null) {
         co.toast("请选择规格")
      } else if (_this.data.inventory < this.data.numbers) {
         co.toast("库存不足")
      } else {
         var select = _this.data.detail
         var carlist = {
            Products_ID: select.Products_ID,
            Products_Photo: select.Products_JSON.ImgPath[0],
            Products_Name: select.Products_Name,
            Products_Price: _this.data.unitPrice,
            num: _this.data.numbers,
            sku_name: select.sku_list[_this.data.id].Sku_Name,
            sku_id: _this.data.id,
            Biz_ID: "91",
            UserName: app.Data.userInfo.User_NickName,
            unionid: app.Data.userInfo.unionid
         }
         var param = {
            // UserName: app.Data.userInfo.User_NickName,
            // unionid: app.Data.userInfo.unionid,
            User_ID: app.Data.userInfo.User_ID,
            Products: JSON.stringify(carlist),
            Biz_ID:91
         }
         co.getMealApi("jyhs/Cartmg/productsToShopCart", param, function(res) {
            if (res.code == 0) {
               co.toast("成功加入购物车")
               _this.setData({
                  showModal: false
               })
            } else {
               co.toast("加入购物车失败")
            }
         })
      }
   },
   // 添加收藏
   addToCallect: function() {
      if (!app.Data.userInfo) {
         wx.navigateTo({
            url: '/06login/login?id=addCart',
         })
         return
      }
      var param = {
         User_ID: this.data.user.User_ID,
         Products_ID: this.data.detail.Products_ID,
      }
      co.getMealApi('favourite/Famg/insertFavourite', param, function(res) {
         _this.getProductList()
         if (res.code == 0) {
            co.toast("收藏成功")
         } else {
            co.toast("收藏失败")
         }
      })
   },
   // 取消收藏
   cancelCallect: function() {
      if (!app.Data.userInfo) {
         wx.navigateTo({
            url: '/06login/login?id=addCart',
         })
         return
      }
      var param = {
         User_ID: this.data.user.User_ID,
         Products_ID: this.data.detail.Products_ID,
      }
      co.getMealApi('favourite/Famg/deleteFavourite', param, function(res) {
         _this.getProductList()
         if (res.code == 0) {
            co.toast("取消收藏成功")
         } else {
            co.toast("取消收藏失败")
         }
      })
   },
   // 获取商品的评论
   getProductComment() {
      var param = {
         Product_ID: Products_ID
      }
      co.getMealApi('product/Pmg/getComment', param, function(res) {
         if (res.code == 0) {
            _this.setData({
               comment: res.data
            })
         }
         if (res.msg == "此商品暂无评论") {
            _this.setData({
               isDisplay: res.msg
            })
         } else {
            _this.setData({
               isDisplay: false
            })
         }
      })
   },
   // 数量加减
   increase: function() {
      var num = _this.data.numbers
      num++
      this.setData({
         numbers: num
      });
   },
   reduce: function() {
      var num = _this.data.numbers
      if (num > 1) {
         num--
      }
      this.setData({
         numbers: num
      });
   },
   // 手动输入数量
   alterNumbers: function(e) {
      var num = e.detail.value
      this.setData({
         numbers: num
      })
   },
   // 跳转到购物车
   toCoupon: function() {
      wx.navigateTo({
         url: '/04me/04functionalArea/12coupon/12coupon'
      })
   },
   /**
    * 弹窗
    */
   showDialogBtn: function(e) {
      if (!app.Data.userInfo) {
         wx.navigateTo({
            url: '/04myCenter/myCenter',
         })
         return
      }
      var option = e.currentTarget.dataset.option;
      console.log(e)
      console.log("【option=====】", option)
      console.log('sku_list', this.data.detail.sku_list.length)
      if (this.data.detail.sku_list.length == 0) {
         if (option == "shoppingCart") {
            this.addShoppingCart()
         } else {
            this.ToOrder()
         }
      } else {
         var obj1 = _this.data.detail
         this.setData({
            unitPrice: obj1.Products_PriceA,
            inventory: obj1.Products_Count,
            showModal: true,
            option: option
         })
      }
   },
   /**
    * 弹出框蒙层截断touchmove事件
    */
   preventTouchMove: function() {},
   /**
    * 隐藏模态对话框
    */
   hideModal: function() {
      this.setData({
         showModal: false
      });
   },
   /**
    * 对话框取消按钮点击事件
    */
   onCancel: function() {
      this.hideModal();
   },
   /**
    * 对话框确认按钮点击事件
    */
   onConfirm: function() {
      this.hideModal();
   },
   //分享
   onShareAppMessage: function() {
      var urlParam = '?Products_ID=' + this.data.detail.Products_ID
      if (app.Data.userInfo.User_ID) {
         urlParam += '&User_ID=' + app.Data.userInfo.User_ID
      }
      var title = this.data.detail.Products_Name
      if (!co.empty(this.data.detail.Biz_Name)) {
         title += '【' + this.data.detail.Biz_Name + '】'
      }
      var share = {
         title: title + '[爱购尤品]',
         path: '/01home/02product/02detail/detail' + urlParam,
         success: function(res) {
            console.log("【分享成功res】===", res)
            var pages = getCurrentPages() //获取加载的页面
            var currentPage = pages[pages.length - 1] //获取当前页面的对象
            app.getShareInfo(res, _this.data.detail.Products_Name + '-商品详情页', currentPage.route)
         }
      }
      console.log('【share】===', share)
      return share;
   }
})